package org.mspring.platform.api.rss.writers;

import electric.xml.Element;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.mspring.platform.api.rss.elements.DublinCore;

/**
 * 
 * @author Gao Youbo
 * @since 2013年7月26日
 */
public abstract class GenericRSSWriter implements RSSWriter {
    static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'kk:mmZZZZZ");

    protected static String stringValue(Object paramObject) {
        String str = "";
        if (paramObject != null)
            if ((paramObject instanceof Date))
                str = sdf.format((Date) paramObject);
            else
                str = paramObject.toString();
        return str;
    }

    protected static void addDublinCore(Element paramElement, DublinCore paramDublinCore) {
        addNSElement(paramElement, "dc", "contributor", paramDublinCore.getDcContributor());
        addNSElement(paramElement, "dc", "coverage", paramDublinCore.getDcCoverage());
        addNSElement(paramElement, "dc", "creator", paramDublinCore.getDcCreator());
        addNSElement(paramElement, "dc", "date", paramDublinCore.getDcDate());
        addNSElement(paramElement, "dc", "description", paramDublinCore.getDcDescription());
        addNSElement(paramElement, "dc", "format", paramDublinCore.getDcFormat());
        addNSElement(paramElement, "dc", "identifier", paramDublinCore.getDcIdentifier());
        addNSElement(paramElement, "dc", "language", paramDublinCore.getDcLanguage());
        addNSElement(paramElement, "dc", "publisher", paramDublinCore.getDcPublisher());
        addNSElement(paramElement, "dc", "relation", paramDublinCore.getDcRelation());
        addNSElement(paramElement, "dc", "rights", paramDublinCore.getDcRights());
        addNSElement(paramElement, "dc", "source", paramDublinCore.getDcSource());
        addNSElement(paramElement, "dc", "subject", paramDublinCore.getDcSubject());
        addNSElement(paramElement, "dc", "title", paramDublinCore.getDcTitle());
        addNSElement(paramElement, "dc", "type", paramDublinCore.getDcType());
    }

    protected void addAttribute(Element paramElement, String paramString, Object paramObject) {
        addAttribute(paramElement, paramString, paramObject, false);
    }

    protected void addAttribute(Element paramElement, String paramString, Object paramObject, boolean paramBoolean) {
        if ((paramBoolean) && (paramObject == null))
            throw new IllegalArgumentException("attribute " + paramString + " must have data in " + paramElement.getName());
        if (paramObject != null) {
            String str = stringValue(paramObject);
            paramElement.setAttribute(paramString, str);
        }
    }

    protected static void addNSAttribute(Element paramElement, String paramString1, String paramString2, Object paramObject) {
        addNSAttribute(paramElement, paramString1, paramString2, paramObject, false);
    }

    protected static void addNSAttribute(Element paramElement, String paramString1, String paramString2, Object paramObject, boolean paramBoolean) {
        if ((paramBoolean == true) && (paramObject == null))
            throw new IllegalArgumentException("attribute " + paramString1 + ":" + paramString2 + " must have data in " + paramElement.getName());
        if (paramObject != null) {
            String str = stringValue(paramObject);
            paramElement.setAttribute(paramString1, paramString2, str);
        }
    }

    protected static Element addNSElement(Element paramElement, String paramString1, String paramString2, Object paramObject) {
        return addNSElement(paramElement, paramString1, paramString2, paramObject, false);
    }

    protected static Element addNSElement(Element paramElement, String paramString1, String paramString2, Object paramObject, boolean paramBoolean) {
        Element localElement = null;
        if ((paramBoolean == true) && (paramObject == null))
            throw new IllegalArgumentException("Element " + paramString1 + ":" + paramString2 + " must have data in " + paramElement.getName());
        if (paramObject != null) {
            String str = stringValue(paramObject);
            localElement = paramElement.addElement(paramString1, paramString2);
            localElement.setString(str);
        }
        return localElement;
    }

    protected static Element addElement(Element paramElement, String paramString, Object paramObject, boolean paramBoolean) {
        Element localElement = null;
        if ((paramBoolean == true) && (paramObject == null))
            throw new IllegalArgumentException("Element " + paramString + " must have data in " + paramElement.getName());
        if (paramObject != null) {
            String str = stringValue(paramObject);
            localElement = paramElement.addElement(paramString);
            localElement.setString(str);
        }
        return localElement;
    }

    protected static Element addElement(Element paramElement, String paramString, Object paramObject) {
        return addElement(paramElement, paramString, paramObject, false);
    }
}